package com.bootdo.rate.controller;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.R;
import com.bootdo.rate.domain.RateDO;
import com.bootdo.rate.serivce.RateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Desc 费率管理
 * @date 2019/12/23
 *
 */
@Controller
@RequestMapping("/rate")
public class RateController {

    private final String PREFIX = "rate";
    @Autowired
    private RateService rateService;

    @RequiresPermissions("rate:rate:edit")
    @Log("设置费率")
    @GetMapping("/rate/{id}")
    String editRate(Model model, @PathVariable("id") Long id) {
        model.addAttribute("userId", id);
        model.addAttribute("agentRate", rateService.findPayeeAgentRate(id));
        model.addAttribute("rate", rateService.edit(id));
        return PREFIX + "/edit";
    }

    @IpFilter
    @RequiresPermissions("rate:rate:edit")
    @Log("设置费率")
    @PostMapping("/rate/save")
    @ResponseBody
    R saveRate(RateDO rate) {
        if (rateService.save(rate) > 0) {
            return R.ok();
        }
        return R.error();
    }
}
